package com.study.java8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author ysy
 * @version 1.0
 * @Title: MethodCitetEST
 * @Note <b>ProjectName:</b> cusc-venus
 * <br><b>PackageName:</b> com.cusc.venus.acc.center.util
 * <br><b>ClassName:</b>
 * <br><b>Date:</b>
 *
 */
public class MethodCiteTest {

    // 方法引用测试类
    public static void main(String[] args) {
        // 1. 无参构造器
        final Car car = Car.create(Car::new);
        final List<Car> cars = Arrays.asList(car);

        // 2. 静态方法：参数须是Car对象
        cars.forEach(Car::collide);

        // 3. 无参成员方法
        cars.forEach(Car::repair);

        // 4. 有参成员方法：参数须是Car对象【前面的police是Car的实例对象】
        final Car police = Car.create(Car::new);
        cars.forEach(police::follow);
    }

    static class Car {
        public static Car create(final Supplier< Car > supplier) {
            return supplier.get();
        }

        public static void collide(final Car car) {
            System.out.println( "Collided " + car.toString() );
        }

        public void follow(final Car another) {
            System.out.println( "Following the " + another.toString() );
        }

        public void repair() {
            System.out.println( "Repaired " + this.toString() );
        }
    }
}
